package com.lain.wx.utils;

import com.lain.wx.vo.TextMessage;
import com.thoughtworks.xstream.XStream;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Title:       MessageUtil Description:
 *
 * @author : liugaoyang date         : 2019/11/14 16:29
 */
public class MessageUtil {

    /**
     * xml转Map
     *
     * @param request
     * @return
     */
    public static Map<String, String> xmlToMap(HttpServletRequest request) {
        Map<String, String> map = new HashMap<>();
        SAXReader reader = new SAXReader();
        ServletInputStream in = null;
        try {
            in = request.getInputStream();
            Document doc = reader.read(in);
            Element root = doc.getRootElement();
            List<Element> list = root.elements();
            for (Element e : list) {
                map.put(e.getName(), e.getText());
            }
            in.close();
        } catch (Exception e) {

        }
        return map;
    }

    /**
     * 文本对象转换成xml
     *
     * @param message
     * @return
     */
    public static String textMessageToXml(Object message) {
        XStream xStream = new XStream();
        xStream.alias("xml", message.getClass());
        return xStream.toXML(message);
    }

    /**
     * 初始化文本信息
     *
     * @param toUserName
     * @param fromUserName
     * @param msgType
     * @param content
     * @return
     */
    public static String initText(String toUserName, String fromUserName, String msgType, String content) {
        TextMessage textMessage = new TextMessage();
        textMessage.setFromUserName(toUserName);
        textMessage.setToUserName(fromUserName);
        textMessage.setMsgType(msgType);
        textMessage.setCreateTime(System.currentTimeMillis());
        textMessage.setContent(content);
        return MessageUtil.textMessageToXml(textMessage);
    }

}
